package com.tansuo365.test1.service.otherservice.impl;

import com.alibaba.fastjson.JSONObject;
import com.tansuo365.test1.bean.kucunbean.DaohuoDetails;
import com.tansuo365.test1.bean.kucunbean.DaohuoZong;
import com.tansuo365.test1.bean.otherbean.ShengouDetails;
import com.tansuo365.test1.bean.otherbean.ShengouZhixing;
import com.tansuo365.test1.bean.otherbean.ShengouZong;
import com.tansuo365.test1.mapper.kucunmapper.DaohuoDetailsMapper;
import com.tansuo365.test1.mapper.kucunmapper.DaohuoZongMapper;
import com.tansuo365.test1.mapper.othermapper.ShengouZhixingMapper;
import com.tansuo365.test1.mapper.othermapper.ShengouZongMapper;
import com.tansuo365.test1.service.otherservice.ShengouZhixingService;
import com.tansuo365.test1.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ShengouZhixingServiceImpl implements ShengouZhixingService {

    @Resource
    private ShengouZhixingMapper shengouZhixingMapper;
    @Resource
    private ShengouZongMapper shengouZongMapper;
    @Resource
    private DaohuoZongMapper daohuoZongMapper;
    @Resource
    private DaohuoDetailsMapper daohuoDetailsMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return shengouZhixingMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(ShengouZhixing shengouZhixing) {
        return shengouZhixingMapper.insertSelective(shengouZhixing);
    }

    /**
     * 到货关联申购单
     *
     * @param daohuoDetailsList
     * @param daohuoZong
     * @return
     */
    @Override
    @Transactional
    public int saveDaohuoBindShengou(List<DaohuoDetails> daohuoDetailsList, DaohuoZong daohuoZong) {
        int isBindSave = 0;
        String shengoudanhaoBind = "";
        int i = 0;
        int insertResultCode = 0;
        for (DaohuoDetails daohuoDetails : daohuoDetailsList) {
            //如果到货数据详细有对应申购单号(关联操作)
            if (StringUtil.isNotEmpty(daohuoDetails.getDuiyingshengoudanhao())) {
                //根据单号和物料名称获取申购执行的数据元组
                //只要有一个数据是从关联申购单来的就属于关联保存
                isBindSave ++;
                shengoudanhaoBind = daohuoDetails.getDuiyingshengoudanhao();
                ShengouZhixing shengouZhixingData = shengouZhixingMapper.listShengouZhixingByDanhaoAndMingcheng(
                        daohuoDetails.getDuiyingshengoudanhao(), daohuoDetails.getMingchengguige());
                System.out.println("shengouZhixingData:"+JSONObject.toJSONString(shengouZhixingData));
                ShengouZhixing shengouZhixing = new ShengouZhixing();
                //shezhi canshu
                shengouZhixing.setDuiyingshengoudanhao(daohuoDetails.getDuiyingshengoudanhao());
                shengouZhixing.setDalei(daohuoDetails.getDalei());
                shengouZhixing.setZhuanye(daohuoDetails.getZhuanye());
                shengouZhixing.setWuliaobianma(daohuoDetails.getWuliaobianma());
                shengouZhixing.setMingchengguige(daohuoDetails.getMingchengguige());
                shengouZhixing.setDanwei(daohuoDetails.getDanwei());
                Double shengoushuliang = daohuoDetails.getNumber();//申购数量
                Double daohuoshuliang = daohuoDetails.getDaohuoshuliang();//到货数量
                Double weidaohuoliang = shengoushuliang - daohuoshuliang;
                shengouZhixing.setShengoushuliang(shengoushuliang);
                shengouZhixing.setDaohuoshuliang(daohuoshuliang);
                shengouZhixing.setWeidaohuoliang(weidaohuoliang);
                shengouZhixing.setBeizhu(daohuoDetails.getBeizhu());
                shengouZhixing.setXiangmumingcheng(daohuoZong.getXiangmumingcheng());
                shengouZhixing.setFounder(daohuoZong.getFounder());
                String caigouzhuangtai = "";
                double preErrorKey = 1e-6; //1*10的-6次方即0.000001
                double lessErrorKey = 1e-4;
                double key1 = 0.0000001d;
                double key2 = 0d;
                //如果这条关联的已经出现在了数据库里,那么说明需要做到货量的累加和未到货量及采购状态更改
                //如果没有,如下,则进行新增
                if (shengouZhixingData == null) {
                    i++;
//                System.out.println(key1==key2); //结果是flase
//                System.out.println(Math.abs(key1-key2)<preErrorKey);
                    if (Math.abs(daohuoshuliang - shengoushuliang) < preErrorKey && (weidaohuoliang == 0)) {
                        caigouzhuangtai = "全部采购";
                    } else if ((daohuoshuliang == 0) && (shengoushuliang != 0)) {
                        caigouzhuangtai = "未采购";
                    } else if ((daohuoshuliang < shengoushuliang) && daohuoshuliang > 0) {
                        caigouzhuangtai = "部分采购";
                    } else if (daohuoshuliang > shengoushuliang) {
                        caigouzhuangtai = "超量采购";
                    }
                    shengouZhixing.setCaigouzhuangtai(caigouzhuangtai);
                    int i1 = shengouZhixingMapper.insertSelective(shengouZhixing);
                    insertResultCode += i1;
                } else {
                    //如果不为空,则说明这条申购数据已经在申购执行里,需要进行update
                    i++;
                    System.out.println("进入到货量累计,分批次到货...");
//                System.out.println(key1==key2); //结果是flase
//                System.out.println(Math.abs(key1-key2)<preErrorKey);
                    if (Math.abs(((daohuoshuliang) + shengouZhixingData.getDaohuoshuliang()) - (shengoushuliang)) < preErrorKey && (shengouZhixingData.getShengoushuliang()) > 0) {
                        System.out.println("全部采购,分批次,获取到的到货量累计:"+daohuoshuliang +"+"+shengouZhixingData.getDaohuoshuliang());
                        caigouzhuangtai = "全部采购";
                    } else if ((daohuoshuliang + shengouZhixingData.getDaohuoshuliang() == 0) && (shengoushuliang != 0)) {
                        System.out.println("未采购,分批次,获取到的到货量累计:"+daohuoshuliang +"+"+shengouZhixingData.getDaohuoshuliang());
                        caigouzhuangtai = "未采购";
                    } else if ((daohuoshuliang + shengouZhixingData.getDaohuoshuliang() < shengoushuliang) && daohuoshuliang > 0) {
                        System.out.println("部分采购,分批次,获取到的到货量累计:"+daohuoshuliang +"+"+shengouZhixingData.getDaohuoshuliang());
                        caigouzhuangtai = "部分采购";
                    } else if (daohuoshuliang + shengouZhixingData.getDaohuoshuliang() > shengoushuliang) {
                        System.out.println("超量采购,分批次,获取到的到货量累计:"+daohuoshuliang +"+"+shengouZhixingData.getDaohuoshuliang());
                        caigouzhuangtai = "超量采购";
                    }
                    shengouZhixing.setCaigouzhuangtai(caigouzhuangtai);
                    shengouZhixing.setId(shengouZhixingData.getId());
                    int i1 = shengouZhixingMapper.updateZhixingChangeDaohuoWeidaohuoAndZhuangtai(shengouZhixing);
                    insertResultCode += i1;
                }
            } else {
                //没有对应申购单号 则不计入申购执行汇总
                i++;
                insertResultCode ++;
            }
        }
        int updateCode = 0;
        //属于绑定保存,更新candelete字段到1(已关联)
        if(isBindSave>0){
            updateCode = shengouZongMapper.updateCanDeleteByDanhao(shengoudanhaoBind);
        }
        if (insertResultCode == i) {
            return i;
        } else if ((insertResultCode != i) && (insertResultCode > 0 && i > 0)) {
            return -1;
        } else if (insertResultCode == 0) {
            return 0;
        }else if(!(updateCode>0)) {
            return -99;
        }else {
            return -2;
        }
    }

    @Override
    @Transactional
    public int deleteDaohuoBindShengou(Integer[] ids) throws Exception{
        int changeCode = 0;
        int candeleteCode = 0;
        for (Integer id : ids) {
            //根据到货单id总 获取到货详细子表(获取子表中的数据)
            List<DaohuoDetails> daohuoDetailsList = daohuoDetailsMapper.listDaohuoDetailsByZongId(id);
            for(DaohuoDetails daohuoDetails :daohuoDetailsList) {
                //这里是全部的daohuoDetails,获取其中的对应申购单号,对申购单总账字段进行更改按需
                String duiyingshengoudanhao = daohuoDetails.getDuiyingshengoudanhao();
                ShengouZhixing shengouZhixing = shengouZhixingMapper.listShengouZhixingByDanhaoAndMingcheng(
                        duiyingshengoudanhao, daohuoDetails.getMingchengguige());

                String caigouzhuangtai = "";
                double preErrorKey = 1e-6; //1*10的-6次方即0.000001
                double key1 = 0.0000001d;
                double key2 = 0d;
                Double shengoushuliang = shengouZhixing.getShengoushuliang();//申购数量
                Double daohuoshuliangZhixing = shengouZhixing.getDaohuoshuliang();//到货数量
                Double daohuoshuliang = daohuoDetails.getDaohuoshuliang();//到货数量
                Double weidaohuoliang = shengoushuliang - daohuoshuliang;
                if (Math.abs((daohuoshuliangZhixing - daohuoshuliang) - shengoushuliang) < preErrorKey && (weidaohuoliang == 0)) {
                    caigouzhuangtai = "全部采购";
                } else if ((daohuoshuliangZhixing - daohuoshuliang < preErrorKey) && (shengoushuliang != 0)) {
                    //原本的到货数量-这次要删除的到货数量单子,如果数量一样 相减=0 则为未采购
                    caigouzhuangtai = "未采购";
                } else if ((daohuoshuliangZhixing -daohuoshuliang < shengoushuliang) && daohuoshuliang > 0) {
                    caigouzhuangtai = "部分采购";
                } else if (daohuoshuliangZhixing - daohuoshuliang> shengoushuliang) {
                    caigouzhuangtai = "超量采购";
                }
                shengouZhixing.setDaohuoshuliang(-daohuoshuliang); //注意是-的
                shengouZhixing.setCaigouzhuangtai(caigouzhuangtai);
                shengouZhixing.setId(shengouZhixing.getId());

                int i = shengouZhixingMapper.updateZhixingChangeDaohuoWeidaohuoAndZhuangtai(shengouZhixing);
                changeCode += i;
//                //duiyingshengoudanhao,查看wuliaodaohuodanmingxi是否还有这个duiyingshengoudanhao,如果没有则更改申购总账candelete,如果有则不做更改
//                List<DaohuoDetails> candeleteDaohuoDetails = daohuoDetailsMapper.listDaohuoDetailsByDuiyingShengoudan(duiyingshengoudanhao);
//                //为空,则说明没有这个对应申购单的到货了,则这个申购单改为可删除,未关联
//                if(candeleteDaohuoDetails==null){
//                    candeleteCode += shengouZongMapper.updateCanDeleteByDanhao(duiyingshengoudanhao);
//                }
            }
        }
        //不能够判定candeleteCode,因为其也许只是删除了一些,而不是删除全部
        if (changeCode > 0) {
            return changeCode;
        } else {
            return 0;
        }
    }

    @Override
    @Transactional
    public int saveShengouToZhixing(List<ShengouDetails> shengouDetailsList, ShengouZong shengouZong) {
        int i = 0;
        int insertResultCode = 0;
        for (ShengouDetails shengouDetails : shengouDetailsList) {
            i++;
            ShengouZhixing shengouZhixing = new ShengouZhixing();
            //shezhi canshu
            shengouZhixing.setDuiyingshengoudanhao(shengouZong.getWuliaoshengoudanhao());
            shengouZhixing.setDalei(shengouDetails.getDalei());
            shengouZhixing.setZhuanye(shengouDetails.getZhuanye());
            shengouZhixing.setWuliaobianma(shengouDetails.getWuliaobianma());
            shengouZhixing.setMingchengguige(shengouDetails.getMingchengguige());
            shengouZhixing.setDanwei(shengouDetails.getDanwei());
            Double shengoushuliang = shengouDetails.getNumber();//申购数量
            shengouZhixing.setShengoushuliang(shengoushuliang);
            shengouZhixing.setDaohuoshuliang(0.0);
            shengouZhixing.setWeidaohuoliang(shengoushuliang);
            shengouZhixing.setBeizhu(shengouDetails.getBeizhu());
            shengouZhixing.setXiangmumingcheng(shengouZong.getXiangmumingcheng());
            shengouZhixing.setFounder(shengouZong.getFounder());
            double preErrorKey = 1e-6; //1*10的-6次方即0.000001
            double key1 = 0.0000001d;
            double key2 = 0d;
            String caigouzhuangtai = "未采购";
            shengouZhixing.setCaigouzhuangtai(caigouzhuangtai);
            int i1 = shengouZhixingMapper.insertSelective(shengouZhixing);
            insertResultCode += i1;
        }
        if (insertResultCode == i) {
            return i;
        } else if ((insertResultCode != i) && (insertResultCode > 0 && i > 0)) {
            return -1;
        } else if (insertResultCode == 0) {
            return 0;
        } else {
            return -2;
        }
    }

    @Override
    public ShengouZhixing selectByPrimaryKey(Integer id) {
        return shengouZhixingMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ShengouZhixing shengouZhixing) {
        return shengouZhixingMapper.updateByPrimaryKeySelective(shengouZhixing);
    }

    @Override
    public List<ShengouZhixing> listShengouZhixingSelective(ShengouZhixing shengouZhixing) {
        return shengouZhixingMapper.listShengouZhixingSelective(shengouZhixing);
    }

    @Override
    public int deleteByIdArr(Integer[] ids) {
        return shengouZhixingMapper.deleteByIdArr(ids);
    }

    @Override
    public int deleteByShengoudanhao(List<String> duiyingshengoudans) {
        return shengouZhixingMapper.deleteByShengoudanhao(duiyingshengoudans);
    }
}
